//
//  MACD.m
//  AppFinance
//
//  Created by Lion on 12-8-19.
//
//

#import "AFMACD.h"

#import "AFSetting.h"

static AFMACD *_macd = nil;

static float kminMacd;
static float kmaxMacd;

static float kdifLast;
static float kmacdLast;
static float kdmLast;

@implementation AFMACD

+(AFMACD*) app{
    if (_macd == nil) {
        _macd = [[self alloc] init];
    }
    return _macd;
}

+(float) kpiMaxMacd{
    return kmaxMacd;
}
+(float) kpiMinMacd{
    return kminMacd;
}
+(float) kpiLastDif{
    return kdifLast;
}
+(float) kpiLastMacd{
    return kmacdLast;
}
+(float) kpiLastDm{
    return kdmLast;
}

int macd_iParam[] = {12, 26, 9};

-(void) initKlineArray:(NSArray *)_kLineArray{
    kLineArray = _kLineArray;
    
    kmaxMacd = -100000000;
    kminMacd = 100000000;
    
    macdListArray = nil;
    difListArray = nil;
    dmListArray = nil;
}

-(NSMutableArray *) kpiMacdListArray{
    return macdListArray;
}
-(NSMutableArray *) kpiDifListArray{
    return difListArray;
}
-(NSMutableArray *) kpiDmListArray{
    return dmListArray;
}


// 计算 k 线数据 转换为 本地MACD数据
-(void) macdData:(int)_drawCount{

    difListArray = [NSMutableArray array];
    macdListArray = [NSMutableArray array];
    dmListArray = [NSMutableArray array];
    
    // MACD 原始数据条数小于 44 条，不绘制
    if(nil == kLineArray || [kLineArray count] < 44){
        return;
    }

    int data_count = (int)[kLineArray count];
    for(int i = 0; i < 3; i++)
        if(macd_iParam[i] > data_count || macd_iParam[i] < 1)
            return;
    
    int _begin;
    if (_drawCount > [kLineArray count]) {
        _begin = 0;
    } else {
        _begin = (int)[kLineArray count] - _drawCount;
    }
    NSLog(@" _begin = %i", _begin);
    
    float di = 0.0;
    float a = 0.0;
    float b = 0.0;
    
    //{12, 26, 9};
    float f_0 = 0;
    float f_2 = 2.0;
    float f_13 = 13.0;
    float f_27 = 27.0;
    //float f_10 = 10.0;
    
    float f_12 = 12;
    float f_26 = 26;
    float f_9 = 9;
    
    float para_0 = f_2/f_13;
    float para_1 = f_2/f_27;
    //float para_2 = f_2/f_10;
    
    float sum_0  = 0.0;
    float sum_1  = 0.0;
    float sum_2  = 0.0;
    
    double d02 = 0.20000000000000001;
    
    NSDictionary *dict;
    
    float dif = 0;
    float pre_macd = 0;
    float macd = 0;
    float d_m = 0;
    for(int i = 0; i < data_count; i++) {
        dict = [kLineArray objectAtIndex:i];
        di = [[dict objectForKey:AF_K_MARKET_CLOSE] floatValue];
        
        //NSLog(@" di = %f",di);
        if(i < 12) {
            sum_0 += di;
            //NSLog(@" sum[0] = %f",sum_0);
            a = i != (12 - 1) ? f_0 : (sum_0 / f_12);
        } else {
            a = (di - a) * para_0 + a;
        }
        if(i < 26) {
            sum_1 += di;
            //NSLog(@" sum[1] = %f",sum_1);
            b = i != (26 - 1) ? f_0 : (sum_1 / f_26);
        } else {
            b = (di - b) * para_1 + b;
        }
        
        dif = i < (12 - 1) || i < 26 ? f_0 : a - b;
        
        //NSLog(@"i = %i , a= %f, b = %f,  dif = %f", i, a, b, dif);
        
        if(i < (26 + 9)) {
            
            //NSLog(@"i = %i , sum[2] = %f  ,dif = %f",i, sum_2, dif);
            sum_2 += dif;

            macd = i != (26 + 9 - 1) ? f_0 : sum_2 / f_9;
            if (i == 0) {
                pre_macd = macd;
            }
        } else {
            macd = (float)((double)(dif - pre_macd) * d02) + pre_macd;
        }
        d_m = dif - macd;
        
        pre_macd = macd;

        //[difListArray addObject:[NSNumber numberWithFloat:dif]];
        //[macdListArray addObject:[NSNumber numberWithFloat:macd]];
        //[dmListArray addObject:[NSNumber numberWithFloat:d_m]];
        
        //NSLog(@"i = %i dif = %.4f, macd = %.4f,  d_m = %.4f,",i, dif,macd,d_m);
        
        [difListArray addObject:[NSNumber numberWithFloat:dif]];
        [macdListArray addObject:[NSNumber numberWithFloat:macd]];
        [dmListArray addObject:[NSNumber numberWithFloat:d_m]];

        //if(i >= _begin){
            if (kminMacd > dif) {
                kminMacd = dif;
            }
            if (kmaxMacd < dif) {
                kmaxMacd = dif;
            }
            
            if (kminMacd > macd) {
                kminMacd = macd;
            }
            if (kmaxMacd < macd) {
                kmaxMacd = macd;
            }
            
            if (kminMacd > d_m) {
                kminMacd = d_m;
            }
            if (kmaxMacd < d_m) {
                kmaxMacd = d_m;
            }
        //}
        
        if (i == (data_count - 1)) {
            kdifLast = dif;
            kmacdLast = macd;
            kdmLast = d_m;
        }
        
        //NSLog(@"di = %f dif = %.2f  def = %.2f  macd = %.2f",di,dif,def,macd);
        //NSLog(@"dif = %.4f, macd = %.4f,  d_m = %.4f,",dif,macd,d_m);
    }
    //NSLog(@"dif = %.4f, def = %.4f, macd = %.4f",dif,def,macd);
    
    //NSLog(@" kminMacd = %f, kmaxMacd = %f",kminMacd, kmaxMacd);
    //NSLog(@" kdifLast = %f, kdeaLast = %f , kmacdLast = %f",kdifLast, kdeaLast,kmacdLast);
}

@end
